package com.base.cn.platform.os.controller.web.course.open;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.utils.count.PageFrom;
import com.base.cn.platform.os.common.utils.count.PageKeyUtils;
import com.base.cn.platform.os.common.utils.course.CourseOpenForm;
import com.base.cn.platform.os.service.course.open.CourseOpenBespeakService;
import com.base.cn.platform.os.service.course.open.CourseOpenService;
import com.base.cn.platform.os.service.manage.count.PageViewCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;


/**
 * 前台免费直播Controller
 *
 * @author s.li
 * @create 2018-05-28-18:38
 */
@Controller
@RequestMapping("/web/course/open")
public class WebCourseOpenController extends WebBaseController {

    @Autowired
    private CourseOpenService courseOpenService;
    @Autowired
    private CourseOpenBespeakService courseOpenBespeakService;
    @Autowired
    private PageViewCountService pageViewCountService;


    @RequestMapping("/index")
    public String index(Model model,
                        @RequestParam(value = "openId",required = false) BigDecimal openId){
        //当前正在直播的
        Map<String,Object> nowLive = courseOpenService.findNowLiveCourseOpen();
        model.addAttribute("nowLive",nowLive);
        model.addAttribute("openId",openId);

        pageViewCountService.addPageCount(PageKeyUtils.OPEN_INDEX, PageFrom.PAGE_FROM_PC);
        return resultPage("/course/open/course-open");
    }

    /**
     * 加载直播数据
     * @param model
     * @param params 查询条件
     * @param currentPage 页码
     * @return 数据显示页面
     */
    @RequestMapping("/ajaxLive")
    public String ajaxLive(Model model,
                           HttpServletRequest request,
                           @RequestBody Map<String,Object> params,
                           @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        params.put("status",1);
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        params.put("userId",userId);
        Map<String,Object> dataMap = courseOpenService.findCourseOpenPage(params,currentPage,4,false,false,false,true);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/course/open/ajax-open-list");
    }

    /**
     * 播放免费直播
     * @param request
     * @param id 免费直播ID
     * @return  Map<String,Object>
     */
    @RequestMapping("/playOpenLive")
    @ResponseBody
    public Map<String,Object> playOpenLive(HttpServletRequest request,
                                           @RequestParam("id") BigDecimal id){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return courseOpenService.playOpenLive(id,userId, CourseOpenForm.PC);
    }

    /**
     * 预约报名免费直播
     * @param request HttpServletRequest
     * @param openId 免费直播ID
     * @return Map<String,Object>
     */
    @RequestMapping("/bespeakOpen")
    @ResponseBody
    public Map<String,Object> bespeakOpen(HttpServletRequest request,
                                          @RequestParam("openId") BigDecimal openId){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String,Object> params = new HashMap<>();
        params.put("userId",userId);
        params.put("openCourseId",openId);
        return courseOpenBespeakService.saveBespeak(params);
    }

}
